#encoding=utf-8

from sklearn.datasets import fetch_20newsgroups
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB

def naviebayes1():

    news = fetch_20newsgroups(subset='all')
    x_train, x_test, y_train, y_test = train_test_split(news.data, news.target, test_size=0.25)  #数据分割
    tf = TfidfVectorizer()                        #特征抽取
    x_train = tf.fit_transform(x_train)            #统计重要性
    print(tf.get_feature_names())
    x_test = tf.transform(x_test)
    mlt = MultinomialNB(alpha=1.0)
    print(x_train.toarray())
    mlt.fit(x_train, y_train)
    y_predict = mlt.predict(x_test)
    print('预测：', y_predict)
    acc = mlt.score(x_test, y_test)
    print('准确率:', acc)
    return None


if __name__ == '__main__':
    naviebayes1()